package com.mall.web.annotation;

import java.lang.reflect.Method;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.mall.client.commkey.CommKeyService;
import com.mall.pojo.Commkey;
import com.mall.web.util.HttpUtil;
import com.mall.web.vo.AjaxResult;
import com.mall.web.vo.StatusInfo;

@Aspect
@Component
public class CheckCommmonOnlyKeyAop {
	private static final Logger log = LoggerFactory.getLogger(CheckCommmonOnlyKeyAop.class);

	@Autowired
	private CommKeyService commKeyService;

	@Around("@annotation(com.mall.web.annotation.CheckCommmonOnlyKey)")
	public Object doCheck(ProceedingJoinPoint joinPoint) throws Throwable {
		String methodName = joinPoint.getSignature().getName();
		Method method = joinPoint.getTarget().getClass().getMethod(methodName,
				((MethodSignature) joinPoint.getSignature()).getParameterTypes());
		CheckCommmonOnlyKey securityAnnotation = method.getAnnotation(CheckCommmonOnlyKey.class);

		// 是否必须
		boolean mustValid = securityAnnotation.mustValid();

		// 当前登录用户
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
				.getRequest();
		HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
				.getResponse();

		boolean ajaxMethod = false;
		// 如果方法有@ResponseBody注解，说明该方法是ajax方法
		ResponseBody responseBodyAnnotation = method.getAnnotation(ResponseBody.class);
		if (responseBodyAnnotation != null) {
			ajaxMethod = true;
		}

		if (!mustValid) {
			return joinPoint.proceed();
		}

		Integer commkeyId = HttpUtil.getCommkeyId(request, response);
		if (commkeyId == null || commkeyId <= 0) {
			if (ajaxMethod) {
				AjaxResult failResult = new AjaxResult();
				failResult.setCode(StatusInfo.REQUEST_NOT_LOGIN);
				failResult.setMessage(StatusInfo.FAILURE_CHECK_COMMKEY);
				HttpUtil.ajaxOutput(failResult, response);
				return null;
			}
			response.sendRedirect(HttpUtil.URL_COMMKEY_ERROR);
			return null;

		}

		List<Commkey> commkeyList = commKeyService.getComKeyByCommOnlyKey(commkeyId);
		if (CollectionUtils.isEmpty(commkeyList)) {
			if (ajaxMethod) {
				AjaxResult failResult = new AjaxResult();
				failResult.setCode(StatusInfo.REQUEST_NOT_LOGIN);
				failResult.setMessage(StatusInfo.FAILURE_CHECK_COMMKEY);
				HttpUtil.ajaxOutput(failResult, response);
				return null;
			}
			response.sendRedirect(HttpUtil.URL_COMMKEY_ERROR);
			return null;
		}

		return joinPoint.proceed();

	}
}
